//
//  ANEditAddressController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANEditAddressController.h"
#import "inputCell.h"
#import "AddressDeleteView.h"
#import "AddressEntity.h"
#import "AddressIsDefaultCell.h"

@interface ANEditAddressController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) AddressDeleteView *footerView;

@property (nonatomic, copy) NSString *receContact;//收件人
@property (nonatomic, copy) NSString *receTel;
@property (nonatomic, copy) NSString *receAddress;
@property (nonatomic) BOOL isDefault;

@end

@implementation ANEditAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}

- (void)saveAddress:(id)sender
{
    [self addOrEditAddressRequest];
}

- (void)addOrEditAddressRequest
{
    if (!self.receContact || self.receContact.length < 1) {
        [MBProgressHUD showWarning:@"请输入收件人"];
        return;
    }
    if (!self.receTel || self.receTel.length < 1) {
        [MBProgressHUD showWarning:@"请输入手机号码"];
        return;
    }
    if (!self.receAddress || self.receAddress.length < 1) {
        [MBProgressHUD showWarning:@"请输入详细地址"];
        return;
    }
    
    NSString *addressId = @"";
    NSMutableDictionary *body = @{}.mutableCopy;
    if (self.type == ANEditAddressControllerEdit) {
        addressId = self.addressEntity.addressId;
        body[@"addressId"] = addressId;
        body[@"createTime"] = self.addressEntity.createTime;
    }
    body[@"memberId"] = UserUtils.shared.vipId;
    body[@"receContact"] = self.receContact;
    body[@"receTel"] = self.receTel;
    body[@"receAddress"] = self.receAddress;
    body[@"isDefult"] = self.isDefault?@1:@0;//1默认，0不默认
    body[@"state"] = @1;//0无效，1有效
    [MBProgressHUD showActivityMessage:nil];
    [URLManager.shareManager postUrlStr:Api_Mine_Address_AddOrEdit parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            [MBProgressHUD showError:responseObj[@"message"]];
            if ([responseObj[@"code"] intValue] == 200) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            else {
                
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}



- (void)deleteAddressRequest
{
    NSDictionary *body = @{
                           @"id":self.addressEntity.addressId
                           };
    [MBProgressHUD showActivityMessage:nil];
    [URLManager.shareManager postUrlStr:Api_Mine_Address_Delete parameters:body success:^(NSURLResponse * _Nonnull response, id  _Nullable responseObj, NSDictionary * _Nullable otherResponse) {
        [MBProgressHUD hideHUD];
        if (responseObj) {
            [MBProgressHUD showError:responseObj[@"message"]];
            if ([responseObj[@"code"] intValue] == 200) {
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    } failure:^(NSURLResponse * _Nullable response, NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
    }];
}

- (void)setupSubViews
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAddress:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)setType:(ANEditAddressControllerType)type
{
    _type = type;
    if (type == ANEditAddressControllerEdit) {
        self.footerView = [[AddressDeleteView alloc] initWithFrame:CGRectMake(0, 0, 0, 78)];
        self.tableView.tableFooterView = self.footerView;
        WeakSelf
        self.footerView.deleteHandler = ^{
            [weakSelf deleteAddressRequest];
        };
    }
    else {
        self.tableView.tableFooterView = [UIView new];
    }
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        inputCell *cell = [tableView dequeueReusableInputCellWithIdentifier:@"inputCell"];
        if (indexPath.row == 0) {
            cell.titleLab.text = @"收件人";
            cell.placeHolderString = @"请输入收件人";
            cell.text = self.receContact;
        }
        if (indexPath.row == 1) {
            cell.titleLab.text = @"手机号码";
            cell.placeHolderString = @"请输入手机号码";
            cell.text = self.receTel;
        }
        if (indexPath.row == 2) {
            cell.titleLab.text = @"详细地址";
            cell.placeHolderString = @"请输入详细地址";
            cell.text = self.receAddress;
        }
        return cell;
    }
    else {
        AddressIsDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddressIsDefaultCell"];
        if (!cell) {
            cell = [[AddressIsDefaultCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"AddressIsDefaultCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.defaultHandler = ^(BOOL isDefault) {
            self.isDefault = isDefault;
        };
        cell.isDefault = self.isDefault;
        return cell;
    }
    
}

- (void)tableView:(UITableView *)tableView updataText:(NSString *)text indexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        self.receContact = text;
    }
    if (indexPath.row == 1) {
        self.receTel = text;
    }
    if (indexPath.row == 2) {
        self.receAddress = text;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}

- (void)setAddressEntity:(AddressEntity *)addressEntity
{
    _addressEntity = addressEntity;
    self.receContact = addressEntity.receContact;
    self.receTel = addressEntity.receTel;
    self.receAddress = addressEntity.receAddress;
    self.isDefault = addressEntity.isDefult.boolValue;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 50;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
